﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;

namespace Alvin.Common.Utils
{
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    public static class BroadcastingUtil
    {
        public delegate void BroadCastEventHandler(Broadcaster sender, BroadcastEventArgs e);
        /// <summary>
        /// 广播
        /// </summary>
        public class Broadcaster
        {
            /// <summary>
            /// 当收到数据的时候会产生这个事件
            /// </summary>
            public event BroadCastEventHandler OnReceive;

            /// <summary>
            /// 发送广播事件
            /// </summary>
            public event BroadCastEventHandler OnSend;

            public int Port { get; private set; }
            public void Send(string message)
            {
                using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
                {
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                    var ipEndPoint = new IPEndPoint(IPAddress.Broadcast, Port);
                    try
                    {
                        var bytes = Encoding.UTF8.GetBytes(message);
                        socket.SendTo(bytes, ipEndPoint);
                        if (OnSend!=null)
                        {
                            OnSend.Invoke(this, new BroadcastEventArgs("", message,Port));
                        }
                    }
                    catch (SocketException ex)
                    {
                        
                    }
                    catch (Exception ex)
                    {
                        
                    }
                    finally
                    {
                        Thread.Sleep(1000);
                    }
                }

            }
        }

        /// <summary>
        /// 广播的状态
        /// </summary>
        public enum BroadcastingStates
        {
            /// <summary>
            /// 打开
            /// </summary>
            Open,
            /// <summary>
            /// 正在打开
            /// </summary>
            Openning,
            /// <summary>
            /// 已经打开
            /// </summary>
            Openned,
            /// <summary>
            /// 关闭
            /// </summary>
            Close,
            /// <summary>
            /// 正在关闭
            /// </summary>
            Closing,
            /// <summary>
            /// 已经关闭
            /// </summary>
            Closed
        }

        public class BroadcastEventArgs
        {
            /// <summary>
            /// Ip
            /// </summary>
            public string Ip { get;private set; }

            /// <summary>
            /// 端口号
            /// </summary>
            public int Port { get; private set; }

            /// <summary>
            /// 信息
            /// </summary>
            public string Message { get; private set; }

            public BroadcastEventArgs(string ip, string message, int port)
            {
                Ip = ip;
                Message = message;
                Port = port;
            }
        }
    }
}
